#include <botan/botan.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "SymmetricKeyMemory.h"
#include "SymmetricKeyFile.h"
#include <vector>

using namespace std;

int main(int argc, char** argv)
{
    Botan::LibraryInitializer init;

    /*
    AutoSeeded_RNG rng;

    SymmetricKey symkey(rng, 32);
    InitializationVector iv(rng, 16);

    cout << "Symkey: " << symkey.as_string() << endl;
    cout << "Iv: " << iv.as_string() << endl << endl;

    ofstream output("file.txt.enc", ios::binary);
    ofstream output2("file_dec.txt", ios::binary);
    ifstream input("file.txt", ios::binary);
    ifstream input2("file.txt.enc", ios::binary);

    cout << "Encrypting AES-256, Mode: CBC ..." << endl << endl;

    Keyed_Filter *aes;
    Pipe pipe(aes = get_cipher("AES-256/CBC", symkey, iv, ENCRYPTION), new DataSink_Stream(output));

    byte* buffer = new byte[32];
    pipe.start_msg();
    while (!input.eof())
    {
        input.read((char*)buffer, 32);
        pipe.write(buffer, input.gcount());
    }
    pipe.end_msg();
    output.close();

    cout << "Decrypting AES-256, Mode: CBC ..." << endl;

    Pipe pipe2(aes = get_cipher("AES-256/CBC", symkey, iv, DECRYPTION), new DataSink_Stream(output2));

    pipe2.start_msg();
    while (!input2.eof())
    {
        input2.read((char*)buffer, 32);
        pipe2.write(buffer, input2.gcount());
    }
    pipe2.end_msg();
*/
/*
    ///////////// SYMMETRIC KEY IN MEMORY

    SymmetricKeyMemory key("ABCDE", 5);
    SymmetricKeyMemory keyReusable("ABCDE", 5, true);

    cout << "Using one pass key..." << endl;

    std::vector<char> bytes = key.nextBytes(3);
    cout << string(&bytes.front(), 3) << endl;

    bytes = key.nextBytes(3);
    if (bytes.empty())
        cout << "vector vacio!" << endl;


    cout << "Using reusable key..." << endl;

    bytes = keyReusable.nextBytes(3);
        cout << string(&bytes.front(), 3) << endl;

    bytes = keyReusable.nextBytes(3);
    if (bytes.empty())
        cout << "vector vacio!" << endl;
    else
        cout << string(&bytes.front(), 3) << endl;

    bytes = keyReusable.nextBytes(3);
    if (bytes.empty())
        cout << "vector vacio!" << endl;
    else
        cout << string(&bytes.front(), 3) << endl;

    bytes = keyReusable.nextBytes(10);
    if (bytes.empty())
        cout << "vector vacio!" << endl;
    else
        cout << string(&bytes.front(), 10) << endl;
*/

    ///////////// SYMMETRIC KEY FROM FILE

    SymmetricKeyFile keyfile("key.txt");

    cout << "Using one pass key..." << endl;

    std::vector<char> bytes = keyfile.nextBytes(5);
    cout << string(&bytes.front(), 5) << endl;

    bytes = keyfile.nextBytes(10);
    if (bytes.empty())
        cout << "vector vacio!" << endl;
    else
        cout << string(&bytes.front(), 10) << endl;

    bytes = keyfile.nextBytes(10);
    if (bytes.empty())
        cout << "vector vacio!" << endl;
    else
        cout << string(&bytes.front(), 10) << endl;

    return 0;
}
